package com.xand.jsonrpc.test.server;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xand.jsonrpc.JSONGenericResponse;
import com.xand.jsonrpc.server.JSONRPCManager;
import com.xand.jsonrpc.test.TestClassResponse;

public class RPCServlet extends HttpServlet {
	private JSONRPCManager rpc;
	
	public RPCServlet() throws Exception {
		rpc = new JSONRPCManager(); 
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setStatus(HttpServletResponse.SC_OK);
		
		String data = request.getParameter("data");
		String resp;
		try {
			resp = rpc.invoke(data);
		}catch(Exception e){
			throw new ServletException(e);
		}
		
		response.getWriter().println(resp);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
